package com.outdoor.server.controller.admin;

import com.outdoor.common.constant.JwtClaimsConstant;
import com.outdoor.common.properties.JwtProperties;
import com.outdoor.common.result.PageResult;
import com.outdoor.common.result.Result;
import com.outdoor.common.utils.JwtUtil;
import com.outdoor.pojo.dto.AdminDTO;
import com.outdoor.pojo.dto.AdminLoginDTO;
import com.outdoor.pojo.dto.AdminPageQueryDTO;
import com.outdoor.pojo.entity.Admin;
import com.outdoor.pojo.vo.AdminVO;
import com.outdoor.pojo.vo.AdminLoginVO;
import com.outdoor.server.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/admin")
@Slf4j
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param adminLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO) {
        log.info("登录", adminLoginDTO);
        Admin admin = adminService.login(adminLoginDTO);
        Map<String, Object> map = new HashMap<>();
        map.put(JwtClaimsConstant.ADMIN_ID, admin.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                map
        );
        AdminLoginVO adminLoginVO =AdminLoginVO.builder()
               .id(admin.getId())
               .username(admin.getUsername())
                .name(admin.getName())
               .token(token)
               .build();
        return Result.success(adminLoginVO);
    }
    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增管理员
     * @param adminDTO
     * @return
     */
    @PostMapping()
    public Result add(@RequestBody AdminDTO adminDTO) {
        log.info("新增管理员", adminDTO);
        adminService.add(adminDTO);
        return Result.success();
    }
    /**
     * 分页查询管理员
     * @param adminPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(AdminPageQueryDTO adminPageQueryDTO) {
        log.info("分页查询管理员", adminPageQueryDTO);
        PageResult pageResult = adminService.page(adminPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 根据id查询管理员
     */
    @GetMapping("/{id}")
    public Result<AdminVO> getById(@PathVariable Long id) {
        log.info("根据id查询管理员", id);
        AdminVO adminDetailVO = adminService.getById(id);
        return Result.success(adminDetailVO);
    }
    /**
     * 更新管理员信息
     * @param admin
     * @return
     */
    @PutMapping()
    public Result update(@RequestBody Admin admin) {
        log.info("更新管理员信息", admin);
        adminService.update(admin);
        return Result.success();
    }
    /**
     * 管理员启用与禁用
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,Long id) {
        log.info("更新管理员状态", status,id);
        adminService.updateStatus(status,id);
        return Result.success();
    }

}
